﻿using System;
using System.Text;
using System.Web;

namespace ManualMVC
{
    public class IISHandler : IHttpHandler
    {
        /// <summary>
        /// You will need to configure this handler in the Web.config file of your 
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        public bool IsReusable
        {
            // Return false in case your Managed Handler cannot be reused for another request.
            // Usually this would be false in case you have some state information preserved per request.
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            IGetDevController controller = null;
            if(context.Request.Url.AbsolutePath == "/book")
            {
                controller = new BookController();
            }
            else
            {
                controller = new IndexController();
            }

            context.Response.Write(controller.GetHtml(context.Request));
        }

        public interface IGetDevController
        {
            string GetHtml(HttpRequest request);
        }

        public class IndexController : IGetDevController
        {
            public string GetHtml(HttpRequest request)
            {

                StringBuilder sb = new StringBuilder();
                sb.Append("Hello <strong>GetDev</strong>!<br/>");
                sb.Append("<a href='/book?title=ASP.NET'>ASP.NET book</a><br/>");
                sb.Append("<a href='/book?title=ASP.NETMVC'>ASP.NET MVC book</a><br/>");
                return sb.ToString();
            }
        }



        public class BookController : IGetDevController
        {
            public string GetHtml(HttpRequest request)
            {
                Book model = null;
                if (request.QueryString["title"] == "ASP.NET")
                {
                    model = new Book
                                {
                                    Title = "ASP.NET",
                                    Description = "nice book",
                                    ISBN = "123",
                                    Author = "John Doe"
                                };
                }
                if (request.QueryString["title"] == "ASP.NETMVC")
                {
                    model = new Book
                    {
                        Title = "ASP.NET MVC",
                        Description = "nice modern book",
                        ISBN = "345",
                        Author = "John Doe Junior"
                    };
                }
                if(model == null)
                    return "Book was not found";

                var viewEngine = new BookViewEngine();

                return viewEngine.GetHtml(model);

            }
        }


        public class BookViewEngine
        {
            public string GetHtml(Book model)
            {
                return string.Format(
                    "<h1>{0}</h1>.<p>{1}. ISBN <strong>{2}</strong>.</p> Author {3}", model.Title, model.Description, model.ISBN, model.Author);
                
            }
        }


        public class Book
        {
            public string Title { get; set; }
            public string Description { get; set; }
            public string Author { get; set; }
            public string ISBN { get; set; }
        }

        #endregion
    }
}
